<template>
  <Modal title="增加"
         v-model="modelView"
         :mask-closable="false"
         :fullscreen="false"
         :width="620"
         :closable="false"
         @on-visible-change="changeVisible"
         @on-ok="ok">
    <Form ref="addForm" :model="formData" :label-width="80" :rules="ruleValidate">
      <Row>
        <Col :span="11">
          <FormItem label="登录名:" prop="loginName">
            <Input v-model="formData.loginName"/>
          </FormItem>
        </Col>
        <Col :span="11">
          <FormItem label="用户名:" prop="userName">
            <Input v-model="formData.userName"/>
          </FormItem>
        </Col>
      </Row>
      <Row>
        <Col :span="11">
          <FormItem label="手机号:" prop="phone">
            <Input v-model="formData.phone"/>
          </FormItem>
        </Col>
        <Col :span="11">
          <FormItem label="状态:" prop="state">
            <Select v-model="formData.state" clearable>
              <Option v-for="item in userStateEnum" :key="item.code" :value="item.code">{{item.desc}}</Option>
            </Select>
          </FormItem>
        </Col>
      </Row>
    </Form>
    <div slot="footer">
      <Button type="text" @click="cancel">取消</Button>
      <Button type="primary" :loading="buttonLoading" @click="ok">确定</Button>
    </div>
  </Modal>
</template>

<script>
  import {getUserStateEnum} from '@/enum/SysEnum'

  export default {
    name: "add",
    props: {
      visible: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        userStateEnum: getUserStateEnum(),
        modelView: false,
        formData: {},
        ruleValidate: {
          loginName: [
            {required: true, message: '用户名不能为空', trigger: 'blur'}
          ],
          userName: [
            {required: true, message: '用户名不能为空', trigger: 'blur'}
          ],
          state: [
            {required: true, message: '状态不能为空', trigger: 'change', type: 'number'}
          ]
        },
        buttonLoading: false
      }
    },
    watch: {
      visible(newVal) {
        this.modelView = newVal
      }
    },
    methods: {
      changeVisible() {
        this.formData = {}
        this.$emit('update:visible', this.modelView)
      },
      cancel() {
        this.modelView = false
      },
      ok() {
        const self = this
        self.$refs['addForm'].validate((valid) => {
          if (valid) {
            self.buttonLoading = true
            let newData = JSON.parse(JSON.stringify(self.formData))
            setTimeout(() => {
              self.buttonLoading = false
              self.modelView = false
              self.$Message.success({
                content: '新增成功'
              })
              self.$emit('on-ok', newData)
            }, 1000);
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
